package com.zhiwang.xiaoqu.di.module

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.google.gson.Gson
import com.zhiwang.xiaoqu.mvp.model.RestApi
import com.zhiwang.xiaoqu.mvp.model.RestApiHelper
import com.zhiwang.xiaoqu.mvp.model.TslProvider
import com.zhiwang.xiaoqu.mvp.model.room.AppDataBase
import com.zhiwang.xiaoqu.viewmodel.NetWorkViewModel
import dagger.Module
import dagger.Provides
import javax.inject.Singleton

@Module
class AppModule(var appContext: Application) {

    @Singleton
    @Provides
    fun provideApp(): Application {
        return appContext
    }

    @Singleton
    @Provides
    fun provideContext(): Context {
        return appContext
    }

    @Singleton
    @Provides
    fun tslProvider(): TslProvider {
        return TslProvider(appContext)
    }

    @Singleton
    @Provides
    fun sharePref(): SharedPreferences {
        return PreferenceManager.getDefaultSharedPreferences(appContext)
    }

    @Singleton
    @Provides
    fun provideDatabase(): AppDataBase {
        return AppDataBase.getInstance(appContext)
    }

    @Singleton
    @Provides
    fun provideNetWorkViewModel(): NetWorkViewModel {
        return NetWorkViewModel(appContext)
    }

//    @Singleton
//    @Provides
//    fun provideUserViewModel(context:Context): UserViewModel {
//        return UserViewModel(context)
//    }

    @Singleton
    @Provides
    fun provideGson(): Gson {
        return Gson()
    }


    @Singleton
    @Provides
    fun provideRestApiHelper(gson: Gson, tslProvider: TslProvider): RestApiHelper {
        return RestApiHelper(gson, tslProvider)
    }

    @Singleton
    @Provides
    fun provideRestApi(restAipHelper: RestApiHelper): RestApi {
        return restAipHelper.create()
    }
}